#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unity.h"

#include "mock_bsp_test_commom_rcvData.h"
#include "_RecvBusData_proc.h"

// 测试夹具
static B1553_BusService testBusService;

void setUp(void) {
    // 每个测试用例执行前的初始化
    memset(&testBusService, 0, sizeof(B1553_BusService));
}

void tearDown(void) {
    // 每个测试用例执行后的清理
}

// 测试用例1: 数据包长度不匹配
void test_RecvBusData_proc_Should_ReturnError_When_PacketLengthMismatch(void) {
    // 准备测试数据
    testBusService.TC_CmdData.packLenth = 10;
    testBusService.DistrbTransDesc_bytes = 20; // 不匹配的长度
    
    rt_GetBusCMDData_IgnoreAndReturn(&testBusService);
    // 调用被测函数
    u32 result = _RecvBusData_proc(&testBusService);
    
    // 验证结果
    TEST_ASSERT_EQUAL(CMDHANDLE_DONE, result);
    TEST_ASSERT_EQUAL(TELEMETRY_BUSCMD_LENTH_ERR, testBusService.Telemeter_TX_Data.Comm_Error_Flag);
    TEST_ASSERT_EQUAL(1, testBusService.Telemeter_TX_Data.Comm_Error_Count);
}

// 测试用例2: CRC校验失败
void test_RecvBusData_proc_Should_ReturnError_When_CRCValidationFailed(void) {
    // 准备测试数据
    testBusService.TC_CmdData.packLenth = 10;
    testBusService.DistrbTransDesc_bytes = 17; // 7 + 10 = 17，匹配长度
    testBusService.TC_CmdData.data[0] = 0x1234; // CRC位置的数据
    
    // 设置Mock期望
    rt_GetBusCMDData_IgnoreAndReturn(&testBusService);
    // CRC计算结果不匹配
    iso_8473_checksum_ExpectAndReturn((u8*)&testBusService.TC_CmdData, 15, 0x5678);
    
    // 调用被测函数
    u32 result = _RecvBusData_proc(&testBusService);
    
    // 验证结果
    TEST_ASSERT_EQUAL(CMDHANDLE_DONE, result);
    TEST_ASSERT_EQUAL(TELEMETRY_BUSCMD_ISOSUM_ERR, testBusService.Telemeter_TX_Data.Comm_Error_Flag);
    TEST_ASSERT_EQUAL(1, testBusService.Telemeter_TX_Data.Comm_Error_Count);
}

// 测试用例3: 命令ID为TC_BUSCOMMAND_ID
void test_RecvBusData_proc_Should_CallBusCMDDataProc_When_CommandIDIsBusCommand(void) {
    u32 expectedReturn = 0x12345678;
    
    // 准备测试数据
    testBusService.TC_CmdData.packID = TC_BUSCOMMAND_ID;
    testBusService.TC_CmdData.packLenth = 10;
    testBusService.DistrbTransDesc_bytes = 17; // 7 + 10 = 17，匹配长度
    
    // 设置Mock期望
    rt_GetBusCMDData_IgnoreAndReturn(&testBusService);
    iso_8473_checksum_ExpectAndReturn((u8*)&testBusService.TC_CmdData, 15, testBusService.TC_CmdData.data[0]);
    _BusCMDData_proc_ExpectAndReturn(&testBusService, expectedReturn);
    
    // 调用被测函数
    u32 result = _RecvBusData_proc(&testBusService);
    
    // 验证结果
    TEST_ASSERT_EQUAL(expectedReturn, result);
    TEST_ASSERT_EQUAL(1, testBusService.Telemeter_TX_Data.Comm_ZXZL_RX_Count);
}

// 测试用例4: 命令ID为TC_DATAINJECTION_ID
void test_RecvBusData_proc_Should_CallBusInjectDataProc_When_CommandIDIsDataInjection(void) {
    u32 expectedReturn = 0x87654321;
    
    // 准备测试数据
    testBusService.TC_CmdData.packID = TC_DATAINJECTION_ID;
    testBusService.TC_CmdData.packLenth = 10;
    testBusService.DistrbTransDesc_bytes = 17; // 7 + 10 = 17，匹配长度
    
    // 设置Mock期望
    rt_GetBusCMDData_IgnoreAndReturn(&testBusService);
    iso_8473_checksum_ExpectAndReturn((u8*)&testBusService.TC_CmdData, 15, testBusService.TC_CmdData.data[0]);
    _BusInjectData_proc_ExpectAndReturn(&testBusService, expectedReturn);
    
    // 调用被测函数
    u32 result = _RecvBusData_proc(&testBusService);
    
    // 验证结果
    TEST_ASSERT_EQUAL(expectedReturn, result);
    TEST_ASSERT_EQUAL(1, testBusService.Telemeter_TX_Data.Comm_SJZR_RX_Count);
}

// 测试用例5: 命令ID为未知类型
void test_RecvBusData_proc_Should_ReturnError_When_CommandIDIsUnknown(void) {
    // 准备测试数据
    testBusService.TC_CmdData.packID = 0x999;
    testBusService.TC_CmdData.packLenth = 10;
    testBusService.DistrbTransDesc_bytes = 17; // 7 + 10 = 17，匹配长度
    
    // 设置Mock期望
    rt_GetBusCMDData_IgnoreAndReturn(&testBusService);
    iso_8473_checksum_ExpectAndReturn((u8*)&testBusService.TC_CmdData, 15, testBusService.TC_CmdData.data[0]);
    
    // 调用被测函数
    u32 result = _RecvBusData_proc(&testBusService);
    
    // 验证结果
    TEST_ASSERT_EQUAL(CMDHANDLE_DONE, result);
    TEST_ASSERT_EQUAL(TELEMETRY_BUSCMD_UNDEFINE_ERR, testBusService.Telemeter_TX_Data.Comm_Error_Flag);
    TEST_ASSERT_EQUAL(1, testBusService.Telemeter_TX_Data.Comm_Error_Count);
}

// 测试用例6: 正常流程 - 总线指令
void test_RecvBusData_proc_Should_ExecuteCompleteFlow_When_ValidBusCommand(void) {
    u32 expectedReturn = 0x11111111;
    
    // 准备测试数据
    testBusService.TC_CmdData.packID = TC_BUSCOMMAND_ID;
    testBusService.TC_CmdData.packLenth = 10;
    testBusService.DistrbTransDesc_bytes = 17; // 7 + 8 = 15，匹配长度
    testBusService.TC_CmdData.data[0] = 0xABCD; // 设置CRC值
    
    // 设置Mock期望
    rt_GetBusCMDData_IgnoreAndReturn(&testBusService);
    iso_8473_checksum_ExpectAndReturn((u8*)&testBusService.TC_CmdData, 15, 0xABCD);
    _BusCMDData_proc_ExpectAndReturn(&testBusService, expectedReturn);
    
    // 调用被测函数
    u32 result = _RecvBusData_proc(&testBusService);
    
    // 验证结果
    TEST_ASSERT_EQUAL(expectedReturn, result);
    TEST_ASSERT_EQUAL(1, testBusService.Telemeter_TX_Data.Comm_ZXZL_RX_Count);
    TEST_ASSERT_EQUAL(0, testBusService.Telemeter_TX_Data.Comm_Error_Count);
}